/**
* @file   Player.cpp
* @brief  Implementation file for class Player
* @date   2011-04-04 10:39:20
* @author Yanze Lian
* @email  <yanzel@gmail.com>
*/

#include "stdafx.h"
#include "Player.h"
#include "safe_defs.h"

#pragma comment(lib, "strmiids.lib")



void RecvPlayerMessage(void* pParam, WPARAM inWParam, LPARAM inLParam)
{
	Player* pThis = (Player*)pParam;
	if (pThis == NULL)
		return;
	pThis->ProcessPlayerMessage(inWParam, inLParam);
}


Player::Player(void* pParent, RecvDsMessage fRecvDsMessage)
:m_pParent(pParent),
 m_recvMessageDlg(NULL),
 m_fRecvDsMessage(fRecvDsMessage)
{
	m_recvMessageDlg = new CRecvMessageDlg(this, RecvPlayerMessage);
	m_recvMessageDlg->Create(IDD_DIALOG_RECV_MESSAGE, NULL);
	m_recvMessageDlg->ShowWindow(SW_HIDE);

	m_filterGraph = NULL;
}

Player::~Player()
{
	if (m_recvMessageDlg != NULL)
	{
		m_recvMessageDlg->DestroyWindow();
		delete m_recvMessageDlg;
		m_recvMessageDlg = NULL;
	}
}

bool Player::Save(CString fileName, CString destFileName)
{
	Close();

	m_filterGraph = new CDXGraph();
	if (m_filterGraph->Create())
	{
		HRESULT hr = m_filterGraph->WriteFileWithTitleOverlay(fileName, destFileName);
		bool res = false;
		//bool res = m_filterGraph->SetDisplayWindow(NULL);
		res = m_filterGraph->SetNotifyWindow(m_recvMessageDlg->GetSafeHwnd());
		if (!res)
		{
			g_Log.Error(_T("Player Save SetNotifyWindow Fail"));
		}
		else
		{
			g_Log.Error(_T("Player Save SetNotifyWindow Succeed"));
		}
		//res = m_filterGraph->SetCurrentPosition(0);
		//res = m_filterGraph->Pause();
		return true;
	}
	return false;
}

bool Player::OpenWithoutTitle(CString fileName, HWND hWndDisplay)
{
	Close();

	m_filterGraph = new CDXGraph();
	if (m_filterGraph->Create())
	{
		HRESULT hr = m_filterGraph->RenderFile(fileName);
		bool res = m_filterGraph->SetDisplayWindow(hWndDisplay);
		res = m_filterGraph->SetNotifyWindow(m_recvMessageDlg->GetSafeHwnd());
		res = m_filterGraph->SetCurrentPosition(0);
		res = m_filterGraph->Pause();
		g_Log.Info(_T("Open File %s Succeeded!"), fileName);
		return res;
	}
	return false;
}

bool Player::Open(CString fileName, HWND hWndDisplay)
{
	Close();

	m_filterGraph = new CDXGraph();
	if (m_filterGraph->Create())
	{
		HRESULT hr = m_filterGraph->RenderFileWithVideoRendererOld(fileName);
		//HRESULT hr = m_filterGraph->RenderFileWithTitleOverlay(fileName);
		bool res = m_filterGraph->SetDisplayWindow(hWndDisplay);
		res = m_filterGraph->SetNotifyWindow(m_recvMessageDlg->GetSafeHwnd());
		res = m_filterGraph->SetCurrentPosition(0);
		res = m_filterGraph->Pause();
		g_Log.Info(_T("Open File %s Succeeded!"), fileName);
		return res;
	}
	return false;
}

void Player::Play()
{
	if (m_filterGraph)
	{
		bool res = m_filterGraph->Run();
		if (!res)
		{
			g_Log.Error(_T("Player Play Fail"));
		}
		else
		{
			g_Log.Error(_T("Player Play Succeed"));
		}
	}
}

bool Player::SnapshotBitmap(CString outFile)
{
	if (m_filterGraph)
	{
		return m_filterGraph->SnapshotBitmap(outFile);
	}
	return false;
}

void Player::Pause()
{
	if (m_filterGraph)
	{
		m_filterGraph->Pause();
	}
}

void Player::Stop()
{
	if (m_filterGraph)
	{
		m_filterGraph->SetCurrentPosition(0);
		m_filterGraph->Stop();
	}
}

double Player::GetCurrentPlayTime()
{
	double time = 0;
	if(m_filterGraph)
	{
		m_filterGraph->GetCurrentPosition(&time);
	}
	return time;
}

void Player::Close()
{
	if (m_filterGraph)
	{
		m_filterGraph->Stop();
		m_filterGraph->SetNotifyWindow(NULL);
		m_filterGraph->SetDisplayWindow(NULL);
		delete m_filterGraph;
		m_filterGraph = NULL;
	}
}

void Player::SetScoreFont()
{
	if (m_filterGraph)
	{
		m_filterGraph->SetScoreFont();
	}
}

void Player::ChangeScore(CString strScore)
{
	if (m_filterGraph)
	{
		m_filterGraph->ChangeScore(strScore);
	}
}

IMediaEventEx* Player::GetEventHandle()
{
	if (m_filterGraph)
	{
		return m_filterGraph->GetEventHandle();
	}
	return NULL;
}

void Player::SetCurrentPosition(double inPosition)
{
	if (m_filterGraph)
	{
		m_filterGraph->SetCurrentPosition(0);
	}
}

void Player::OnRecvMessage(MSG_TYPE msg_type)
{
	if (m_fRecvDsMessage != NULL)
	{
		m_fRecvDsMessage(m_pParent, msg_type);
	}
}

void Player::ProcessPlayerMessage(WPARAM inWParam, LPARAM inLParam)
{
	IMediaEventEx * pEvent = GetEventHandle();
	if (pEvent == NULL)
		return;

	LONG   eventCode = 0, eventParam1 = 0, eventParam2 = 0;
	while (SUCCEEDED(pEvent->GetEvent(&eventCode, &eventParam1, &eventParam2, 0)))
	{ 
					HRESULT hr = eventParam1;
		// Spin through the events
		pEvent->FreeEventParams(eventCode, eventParam1, eventParam2);
		switch (eventCode)
		{
		case EC_COMPLETE:
			//Pause();
			//SetCurrentPosition(0);
			OnRecvMessage(COMPLETE);
			break;
		case EC_USERABORT:
			OnRecvMessage(USERABORT);
			break;
		case EC_ERRORABORT:
			//Stop();
			{

			
			CString str;
			str.Format(_T("0x%X"), eventParam1);
			AfxMessageBox(str);
			OnRecvMessage(ERRORABORT);
			break;
			}
		default:
			break;
		}
	}
}